$(function () {
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var fileDialog = $("#client_importData");

    /*列表初始化*/
    client_datagrid.datagrid({
        url: "/client/query.do",
        height: 300,
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        sortName: 'integral',
        sortOrder: 'desc',
        toolbar: "#client_toolbar",
        columns: [[
            {field: 'number', title: '卡号', width: 100},
            {field: 'name', title: '会员姓名', width: 100},
            {field: 'sex', title: '性别', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'level', title: '等级', width: 100,},
            {field: 'email', title: '邮箱', width: 100},
            {field: 'birthday', title: '生日', width: 100},
            {field: 'balance', title: '账户余额', width: 100},
            {field: 'integral', title: '积分', width: 100, sortable: 'true'},
        ]]
    })
    /*编辑弹出框*/
    client_dialog.dialog({
        width: 440,
        height: 270,
        closed: true,
        buttons: [{
            text: "保存",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                methodObj["save"]();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["cancel"]();
            }
        }],
        onClose: function () {
            client_form.form('clear');
        }
    })

    /*导入弹出框*/
    fileDialog.dialog({
        width: 300,
        height: 115,
        closed: true,
        buttons: [{
            text: "上传",
            iconCls: "icon-save",
            plain: true,
            handler: function () {
                $("#file_form").submit();
            }
        }, {
            text: "取消",
            iconCls: "icon-no",
            plain: true,
            handler: function () {
                methodObj["importCancel"]();
            }
        }],
        onClose: function () {
            $("input[name='file']").prop("value", "");
        }
    })

    /*当天生日人数*/
    $.get("/client/bircount.do", function (data) {
        $("#bircount").html(data);
    }, "json");
    //当月过生日会员数
    $.get("/client/bimcount.do", function (data) {
        $("#bimcount").html(data);
    }, "json");

    /*会员总数*/
    $.get("/client/getCount.do",function (data) {
        $("#totalClient").html(data);
    },'json')
    /*平均会员增加量*/
    $.get("/client/getCount.do",function (data) {
        $("#average").html((data/365).toFixed(2));
    },'json')
    /*累计消费*/
    $.get("/client/getTotalExpense.do",function (data) {
        $("#totalExpense").html(data);
    },'json')
    /*共消费比数*/
    $.get("/client/getTotalCount.do",function (data) {
        $("#totalCount").html(data);
    },'json')

    $("#top_datagrid").datagrid({
        url:'/client/getTOP.do',
        fitColumns: true,
        singleSelect: true,
        rownumbers: true,
        showHeader: false,
        scrollbarSize: 0,
        columns: [[
            {field:'client.name', title:'姓名',width: 80, align: 'center',formatter:function (value, row, index) {
                    return row?row.name:'';
                }},
            {
                field: 'totalincome', title: '会员姓名', width: 100, formatter: function (value, row, index) {
                    return value ? "<span style='color: red'>￥" + value + "</span>" : "<span style='color: red'>￥0</span>"
                }
            }

        ]]
    })




    /*方法对象*/
    var methodObj = {
        add: function () {
            client_dialog.dialog('setTitle', "添加会员");
            $("#password_tr").show();
            client_dialog.dialog('open');
        },

        edit: function () {
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            //隐藏密码框
            $("#password_tr").hide();

            //回显数据
            client_form.form("load", row);

            //设置标题
            client_dialog.dialog("setTitle", "编辑会员");
            //打开弹窗
            client_dialog.dialog("open");
        },

        /*保存*/
        save: function () {
            $.messager.confirm("温馨提示", "确定要保存吗?", function (r) {
                if (r) {
                    client_form.form('submit',{
                        url: "/client/saveOrUpdate.do",
                        success: function (data) {
                            data = $.parseJSON(data);
                            if (data.success) {
                                $.messager.alert("温馨提示", "操作成功", "info");
                                client_dialog.dialog('close');
                                client_datagrid.datagrid('reload');
                            } else {
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        }
                    })
                }
            })
        },
        /*编辑取消*/
        cancel:function () {
          client_dialog.dialog('close');
        },
        /*删除*/
        remove: function () {
            var row = client_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据!', 'error');
                return;
            }
            $.messager.confirm("温馨提示", "确定要删除吗", function (r) {
                if (r) {
                    $.post("/client/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", "操作成功", "info")
                            client_datagrid.datagrid('load');
                        } else {
                            $.messager.alert("温馨提示", data.msg, "error")
                        }
                    })
                }
            })
        },
        /*刷新*/
        reload: function () {
            client_datagrid.datagrid('reload');
        },
        /*导入*/
        import: function () {
            fileDialog.dialog('open');
        },
        /*导出*/
        export: function () {
            window.open("/client/exportData.do?" + $("#file_form").serialize())
        },

        /*导入框取消*/
        importCancel: function () {
            fileDialog.dialog('close');
        }
    }

    /*统一事件绑定*/
    $("a[data-method]").click(function () {
        var method = $(this).data("method");
        methodObj[method]();
    })


    /*高级查询事件*/
    $("#keyword").searchbox({
        searcher: function (value, name) {
            client_datagrid.datagrid('load', {
                keyword: value
            })
        }
    })
})
